﻿using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Omu.Awesome.Mvc;
using Omu.AwesomeDemo.Core.Model;
using Omu.AwesomeDemo.Core.Repository;

namespace Omu.AwesomeDemo.WebUI.Controllers
{
    public class CharactersLookupController : LookupController
    {
        private readonly IRepo<Person> repo;

        public CharactersLookupController(IRepo<Person> repo)
        {
            this.repo = repo;
        }

        public override ActionResult SearchForm()
        {
            return Content("");
        }

        public ActionResult Search(IEnumerable<int> selected, int? showId, IEnumerable<int> hobbiesIds)
        {
            var chars = repo.GetAll().Where(o => showId == null || (o.Show != null && o.Show.Id == showId));
            
            chars = chars.Where(o => hobbiesIds == null || (o.Hobbies != null && o.Hobbies.Select(x => x.Id).Intersect(hobbiesIds).Count() != 0));
            
            chars = chars.Where(o =>selected == null || !selected.Contains(o.Id));
            
            return View(@"Awesome\LookupList", chars);

        }

        public ActionResult Selected(IEnumerable<int> selected)
        {
            return View(@"Awesome\LookupList", repo.GetAll().Where(o => selected != null && selected.Contains(o.Id)));
        }

        public ActionResult GetMultiple(IEnumerable<int> selected)
        {
            return Json(repo.GetAll().Where(o => selected.Contains(o.Id)).Select(v => new { Text = v.FirstName + ' ' + v.LastName }));
        }
    }
}